package com.cn.aam.checaiduo.ui.homepage.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.FixLayoutHelper;
import com.alibaba.android.vlayout.layout.FloatLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListV2Listener;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseFragment;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.BannerB;
import com.cn.aam.checaiduo.bean.IndexB;
import com.cn.aam.checaiduo.bean.base.NormalResponseB;
import com.cn.aam.checaiduo.bean.base.PageB;
import com.cn.aam.checaiduo.ui.homepage.adapter.HomeContentAdapter;
import com.cn.aam.checaiduo.ui.homepage.adapter.HomeFloatButtonAdapter;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;


/**
 * monker on 2017/5/19.
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_recyclerView)
    RecyclerView homeRecyclerView;
    private HomeContentAdapter contentAdapter;

    public static HomeFragment newInstance() {
        HomeFragment intance = new HomeFragment();
        return intance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        ToolBarManager.with(mActivity, WMContent)
                .setTitle("悟空车");

        VirtualLayoutManager layoutManager = new VirtualLayoutManager(mActivity);
        homeRecyclerView.setLayoutManager(layoutManager);

        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        homeRecyclerView.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 5);

        DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);
        homeRecyclerView.setAdapter(delegateAdapter);

        List<DelegateAdapter.Adapter> adapters = new LinkedList<>();

        FloatLayoutHelper layoutHelper = new FloatLayoutHelper();
        layoutHelper.setAlignType(FixLayoutHelper.BOTTOM_RIGHT);
        layoutHelper.setDefaultLocation(0, 100);
        VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        adapters.add(new HomeFloatButtonAdapter(mActivity, layoutHelper, 1, layoutParams));
        contentAdapter = new HomeContentAdapter(mActivity, new LinearLayoutHelper(), 1);
        adapters.add(contentAdapter);
        delegateAdapter.setAdapters(adapters);

    }


    /**
     * 时间：2017/6/27
     * 描述：基本信息
     */
    private void getIndexInfo() {
        if (!hasLogin()) {
            contentAdapter.setData(null);
            return;
        }

        RetrofitHome.getInstance()
                .getInternalApi()
                .indexInfo(AppData.getToken())
                .compose(RxSchedulers.<NormalResponseB<IndexB>>toMain())
                .subscribe(new OnRequestListener<NormalResponseB<IndexB>>(mActivity) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(NormalResponseB<IndexB> value) {
                        super.onNext(value);
                        if (value.getIsLogged() == 0) {
                            goLogin();
                            return;
                        }
                        contentAdapter.setData(value.getData());
                    }
                });
    }

    /**
     * @description 获取数据
     */
    private void initBanner() {
        if (contentAdapter != null && contentAdapter.getBannerList() == null)
            RetrofitHome.getInstance()
                    .getInternalApi()
                    .getBannerList("7")
                    .compose(RxSchedulers.<PageB<BannerB>>toMain())
                    .subscribe(new OnRequestListV2Listener<BannerB>(mActivity) {
                        @Override
                        protected boolean isShowProgressBar() {
                            return false;
                        }

                        @Override
                        public void onNext(PageB<BannerB> value) {
                            super.onNext(value);
                            contentAdapter.setBannerList(value.getData());
                        }
                    });
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden && contentAdapter != null) {
            getIndexInfo();
            initBanner();
            if (contentAdapter.getBanner() != null&&contentAdapter.getBannerList() != null)
                contentAdapter.getBanner().startTurning(3000);
        } else {
            if (contentAdapter != null && contentAdapter.getBanner() != null&&contentAdapter.getBannerList() != null) {
                contentAdapter.getBanner().stopTurning();
            }
        }
    }


}
